﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace P.Server
{
    class Program
    {

        private static void RunServer()
        {
            bool restartServer = true;
            while (restartServer)
            {
                SharedLib.Network.Server.Server server = new SharedLib.Network.Server.Server();
                server.Start();
                restartServer = server.RestartServer;
                server.Console.WriteError("Server shutdown");
                
            }
            Environment.Exit(0);
        }

        static void Main()
        {
            if (Debugger.IsAttached)
            {
                RunServer();
            }
            else
            {
                try
                {
                    RunServer();
                }
                catch (Exception e)
                {
                    string logtext = "";
                    if (File.Exists("Servercrashlog.log"))
                    {
                        logtext = File.ReadAllText("Servercrashlog.log");
                    }
                    File.WriteAllText("Servercrashlog.log", logtext + "\r\n" + e.Message + "\r\n\r\n" + e.StackTrace);
                    
                }
            }
        }

        /*
        static void Main(string[] args)
        {
            SharedLib.Network.Class.Server.launchServer();

            while (true)
            {
                string command=Console.ReadLine();
               command= command.Replace(".", ",");
                string[] commands = command.Split(' ');
                if (commands[0] == "setAmbiantValue")
                {
                    foreach (var item in SharedLib.Network.Class.Server.PLAYERS)
                    {
                        SharedLib.Network.Class.Message newMSG = new SharedLib.Network.Class.Message();
                        newMSG._type = SharedLib.Network.Class.MessageType.AMBIANT_VALUE_CHANGED;
                        newMSG._object = (float)Convert.ToDouble( commands[1]);

                        item.addMessage(newMSG);
                    }
                }
            }
        }
        */
    }
}
